#include "CSAllegroController.h"

using namespace DarkStorm2D;
bool CSAllegroController::m_InitTable[NUMBER_INIT_ITEM];
CSAllegroController::CSAllegroController()
{
	for (int i = 0;i < NUMBER_INIT_ITEM;i++)
		m_InitTable[i] = false;

	Init(FLAG_ALLEGRO_INIT);
	Init(FLAG_ALLEGRO_PRIMITIVES_ADDON);
	Init(FLAG_ALLEGRO_INSTALL_KEYBOARD);
}


CSAllegroController::~CSAllegroController()
{
	Uninit(FLAG_ALLEGRO_INIT);
	Uninit(FLAG_ALLEGRO_PRIMITIVES_ADDON);
	Uninit(FLAG_ALLEGRO_INSTALL_KEYBOARD);
}

bool CSAllegroController::Init(int FlagInit)
{
	if (FlagInit >= NUMBER_INIT_ITEM) return false;

	if (GetInit(FlagInit))
	{
		return true;
	}
	else 
	{		
		return SetInit(FlagInit, true);
	}

	return false;
}

bool CSAllegroController::Uninit(int FlagInit)
{
	if (NUMBER_INIT_ITEM >= FlagInit) return false;

	if (!GetInit(FlagInit))
	{
		return true;
	}
	else 
	{		
		return SetInit(FlagInit, false);
	}
}

bool CSAllegroController::SetInit(int FlagInit, bool action)
{
	m_InitTable[FlagInit] = action;

	switch (FlagInit)
	{
	case FLAG_ALLEGRO_INIT:
		if (action)
		{
			return al_install_system(ALLEGRO_VERSION_INT,NULL);
		} 
		else
		{
			al_uninstall_system();	
			return true;
		}
		break;

	case FLAG_ALLEGRO_PRIMITIVES_ADDON:
		if (action)
		{
			return al_init_primitives_addon();
		} 
		else
		{
			al_shutdown_primitives_addon();
			return true;
		}
		break;

		case FLAG_ALLEGRO_IIO_ADDON:
		if (action)
		{
			return al_init_image_addon();
		} 
		else
		{
			al_shutdown_image_addon();
			return true;
		}
		break;

		case FLAG_ALLEGRO_FONT_ADDON:
		if (action)
		{
			al_init_font_addon();
			return true;
		} 
		else
		{
			al_shutdown_font_addon();
			return true;
		}
		break;

		case FLAG_ALLEGRO_TTF_ADDON:
		if (action)
		{
			
			return al_init_ttf_addon();;
		} 
		else
		{
			return true;
		}
		break;

		case FLAG_ALLEGRO_INSTALL_KEYBOARD:
		if (action)
		{
			return al_install_keyboard();
		} 
		else
		{
			al_uninstall_keyboard();
			return true;
		}
		break;

		case FLAG_ALLEGRO_INSTALL_MOUSE:
		if (action)
		{
			return al_install_mouse();
		} 
		else
		{
			al_uninstall_mouse();
			return true;
		}
		break;

		case FLAG_ALLEGRO_INSTALL_JOYSTICK:
		if (action)
		{
			return al_install_joystick();
		} 
		else
		{
			al_uninstall_joystick();
			return true;
		}
		break;

		case FLAG_ALLEGRO_INSTALL_AUDIO:
		if (action)
		{
			return al_install_audio();
		} 
		else
		{
			al_uninstall_audio();
			return true;
		}
		break;

		case FLAG_ALLEGRO_INIT_ACODEC_ADDON:
		if (action)
		{
			return al_init_acodec_addon();
		} 
		else
		{
			return true;
		}
		break;

	}
	return false;
}

bool CSAllegroController::GetInit(int FlagInit)
{
	return m_InitTable[FlagInit];
}
